<template>
  <div>
    <BasicModal @register="registerModal" v-bind="$attrs" @ok="handleSubmit" ok-text="保存">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>

<script setup lang="ts">
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { schemas } from './addAssetModal.data';
  import { modalFormStyleSetting, modalStyleSetting } from '/@/settings/thingsBoardDesign';

  const emit = defineEmits(['success']);

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async () => {
    setModalProps({
      title: '添加资产',
      helpMessage: '查看帮助',
      ...modalStyleSetting,
    });
  });
  const [registerForm, { validate, resetFields }] = useForm({
    layout: 'vertical',
    schemas,
    showActionButtonGroup: false,
    ...modalFormStyleSetting,
  });

  async function handleSubmit() {
    try {
      const values = await validate();
      emit('success', {
        key: 'add',
        values: { ...values },
      });
      closeModal();
      resetFields();
    } catch (error) {}
  }
</script>
